package com.china08.yxyapi.entity.mg.office;

import java.util.List;
import java.util.stream.Collectors;

import javax.persistence.Id;

import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import com.china08.yxyapi.entity.mg.MongoBaseEntityNoId;

/**
 * @author Richard.Wang
 * @descript 班级通知
 */
@Document(collection = "yxy.oa.notice.class")
public class YxyNoticeClass extends MongoBaseEntityNoId {
    private static final long serialVersionUID = -6690320422620877550L;

    @Id
    private String id;

    /** The user id. 发件人id */
    @Indexed
    @Field(value = "user_id")
    private String userId;

    /**
     * 用户头像
     */
    @Field(value = "avatar")
    private String avatar;

    /** The user name. 发件人名称 */
    @Field(value = "nick_name")
    private String nickName;

    /**
     * 班级id,
     */
    @Field(value = "class_id")
    private String classId;

    /**
     * 班级名称
     */
    @Field(value = "class_nick")
    private String classNick;

    @Field(value = "school_id")
    private String schoolId;

    /** The title. 标题 */
    @Field(value = "title")
    private String title;

    /** The content. 内容 */
    @Field(value = "content")
    private String content;

    /**
     * 接收者 学生，存储姓名，id，
     */
    @Field(value = "receiver")
    private List<YxyNotifictionUser> receiver;

    /**
     * 接收率
     */
    private double receiveRate = 0.0;

    /**
     * 图片附件,可以放置多张图片, 6张限制
     */
    @Field(value = "image")
    private List<String> image;

    /** The adjunct. 附件的文件路径,附件原始文件名称。 */
    @Field(value = "attachment_name")
    private String attachmentName;

    @Field(value = "attachment_path")
    private String attachmentPath;

    // 获取已收到人数 1 或 未收到人数 0
    public long getReadManCount(int isRead) {
        long count = receiver.stream().filter(x -> {
            return x.getIsRead() == isRead;
        }).collect(Collectors.counting());
        return count;
    }

    // 获取已收到人员字符串拼接列表 1 或 未收到人员字符串拼接列表 0
    public String getReadManUserNicknameStr(int isRead, String splitStr) {
        StringBuilder readManUserNicknameStr = new StringBuilder();
        for (YxyNotifictionUser yxyNotifictionUser : receiver) {
            if (yxyNotifictionUser.getIsRead() == isRead) {
                readManUserNicknameStr.append(yxyNotifictionUser.getUserNick()).append(splitStr);
            }
        }
        if (readManUserNicknameStr.length() > 0) { //
            return readManUserNicknameStr.substring(0, readManUserNicknameStr.length() - 1).toString();
        }
        return readManUserNicknameStr.toString();
    }

    public class YxyNotifictionUser {

        public String userId;

        public String userNick;
        /**
         * 0:未读 1：已读
         */
        public Integer isRead;

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public String getUserNick() {
            return userNick;
        }

        public void setUserNick(String userNick) {
            this.userNick = userNick;
        }

        public Integer getIsRead() {
            return isRead;
        }

        public void setIsRead(Integer isRead) {
            this.isRead = isRead;
        }

    }

    // getter and setter
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getClassId() {
        return classId;
    }

    public void setClassId(String classId) {
        this.classId = classId;
    }

    public String getSchoolId() {
        return schoolId;
    }

    public void setSchoolId(String schoolId) {
        this.schoolId = schoolId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<YxyNotifictionUser> getReceiver() {
        return receiver;
    }

    public void setReceiver(List<YxyNotifictionUser> receiver) {
        this.receiver = receiver;
    }

    public List<String> getImage() {
        return image;
    }

    public void setImage(List<String> image) {
        this.image = image;
    }

    public String getAttachmentName() {
        return attachmentName;
    }

    public void setAttachmentName(String attachmentName) {
        this.attachmentName = attachmentName;
    }

    public String getAttachmentPath() {
        return attachmentPath;
    }

    public void setAttachmentPath(String attachmentPath) {
        this.attachmentPath = attachmentPath;
    }

    public String getClassNick() {
        return classNick;
    }

    public void setClassNick(String classNick) {
        this.classNick = classNick;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getReceiveRate() {
        return receiveRate;
    }

    public void setReceiveRate(double receiveRate) {
        this.receiveRate = receiveRate;
    }

}
